import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '@/components/HomeView.vue'
import ConLayout from '@/components/ConLayout.vue'
import AutoIdentify from '@/components/AutoIdentify.vue'
import RecordManage from '@/components/RecordManage.vue'
import Person from '@/components/Person.vue'
import UserManage from '@/components/UserManage.vue'
import Password from '@/components/Password.vue'
import New1 from '@/components/New1.vue'
import New2 from '@/components/New2.vue'
import New3 from '@/components/New3.vue'
import New4 from '@/components/New4.vue'
import New5 from '@/components/New5.vue'
import New6 from '@/components/New6.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Layout',
    component: ConLayout,
    redirect: '/home',
    children:[
      {
        path: '/home',
        name: 'home',
        meta: {
          title:'首页'
        },
        component: HomeView
      },
      {
        path: '/identify',
        name: 'AutoIdentify',
        meta: {
          title:'窃漏电识别页面',
          requireAuth: true, // 添加该字段，表示进入这个路由是需要登录的
        },
        component: AutoIdentify
      },
      {
        path: '/record',
        name: 'RecordManage',
        meta: {
          title:'窃漏电识别记录页面',
          requireAuth: true, // 添加该字段，表示进入这个路由是需要登录的
        },
        component: RecordManage
      },
      {
        path: '/person',
        name: 'Person',
        meta: {
          title:'个人信息页面',
        },
        component: Person
      },
      {
        path: '/usermanage',
        name: 'UserManage',
        meta: {
          title:'用户管理页面',
        },
        component: UserManage
      },
      {
        path: '/password',
        name: 'Password',
        meta: {
          title:'修改密码页面',
        },
        component: Password
      },
      {
        path: '/new1',
        name: 'New1',
        meta: {
          title:'新闻',
        },
        component: New1
      },
      {
        path: '/new2',
        name: 'New2',
        meta: {
          title:'新闻',
        },
        component: New2
      },
      {
        path: '/new3',
        name: 'New3',
        meta: {
          title:'新闻',
        },
        component: New3
      },
      {
        path: '/new4',
        name: 'New4',
        meta: {
          title:'新闻',
        },
        component: New4
      },
      {
        path: '/new5',
        name: 'New5',
        meta: {
          title:'新闻',
        },
        component: New5
      },
      {
        path: '/new6',
        name: 'New6',
        meta: {
          title:'新闻',
        },
        component: New6
      },
    ]
  },
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {
    //如果要去往的路由需要登录权限
    if (sessionStorage.getItem('islogin') == 'true') {
      next()
      document.title = to.meta.title
    } else {
      // 未登录，跳转到主页面
      next('/home')
    }
  } else {
    next()
    document.title = to.meta.title
  }
})

export default router
